package com.usefullc.crawler.controller.client;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.crawler.param.TemplateParam;
import com.usefullc.crawler.query.TemplateQuery;
import com.usefullc.crawler.service.ActiveInstanceService;
import com.usefullc.crawler.service.ClientTaskService;
import com.usefullc.crawler.service.CrawlerTaskService;
import com.usefullc.crawler.service.TemplateService;
import com.usefullc.crawler.vo.TemplateVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdParam;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.validator.UsefulValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 爬虫任务控制类
 * @author: 星空
 * @date: 2022-12-15
 */
@RestController
@RequestMapping("/crawlerClient/taskHis/")
@Api( tags="爬虫客户端任务历史")
public class ClientTaskHisController extends BaseController {

    @Autowired
    private TemplateService templateService;
    @Autowired
    private CrawlerTaskService crawlerTaskService;

    @Autowired
    private ActiveInstanceService activeInstanceService;

    @Autowired
    private ClientTaskService clientTaskService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "爬虫模板列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) TemplateQuery query) {
        Pager<TemplateVo> page = templateService.findVoByPage(query);  //todo rpc
        return WebResultBuilder.successResult(page);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "保存爬虫模板", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody TemplateParam param) {
        boolean result = clientTaskService.saveClientTemplate(param);
        return successResult(result);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改爬虫模板", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody TemplateParam param) {
        return successResult(clientTaskService.updateClientTemplate(param));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除爬虫模板",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        templateService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除爬虫模板",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        templateService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "复制爬虫模板", response = WebResult.class)
    @PostMapping(path = "/copy", produces = "application/json")
    @UsefulValid
    public WebResult copy(@UsefulValid(save=true) @RequestBody IdParam idParam) {
        templateService.copy(idParam.getId());
        return successResult();
    }
}
